package com.hefu.virtuallife.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.bean.SmartHomeBean;
import com.hefu.virtuallife.bean.SmartHomeBeanDao;
import com.hefu.virtuallife.utils.SmartDataUtils;

import java.util.List;

/**
 * Created by pc on 2018/7/29.
 */

public class SmartEqAdapter extends BaseAdapter {
    List<SmartHomeBean> list = null;
    private LayoutInflater inflater = null;
    private View view;

    public SmartEqAdapter(Context context, List<SmartHomeBean> list){
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        view = inflater.inflate(R.layout.equipment_list_item, null);

        ImageView iv = (ImageView)view.findViewById(R.id.iv_eq);
        TextView tv_type = (TextView)view.findViewById(R.id.tv_eq_type);
        TextView tv_room = (TextView)view.findViewById(R.id.tv_eq_room);
        CheckBox cb_state = (CheckBox)view.findViewById(R.id.cb_eq_state);

        final SmartHomeBean bean = list.get(position);
        iv.setImageResource(SmartDataUtils.getRes(bean.getType()));
        tv_type.setText(bean.getName());
        tv_room.setText(bean.getRoomType());
        cb_state.setChecked(bean.getState());

        cb_state.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                bean.setState(isChecked);
                //SmartDataUtils.update(bean);
            }
        });
        return view;
    }
}
